<?php

class m_users extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					user_name,
					user_passwd,
					user_login,
					user_logout,
					m_cabang_id,
					m_groupdiv_id,
					user_log,
					s_groups_id,
					groups_name,
					user_id,
                    group_concat(t_project_id) t_project_id
				FROM s_user
					LEFT JOIN s_groups ON groups_id = s_groups_id
					LEFT JOIN t_userproject ON s_users_id = user_id
				WHERE 
					user_active = 'y'
		";
		
		if (@$user_name != "") $sql .= " AND user_name like '%".@$user_name."%'";
		if (@$user_passwd != "") $sql .= " AND user_passwd like '%".@$user_passwd."%'";
		if (@$user_login != "") $sql .= " AND user_login like '%".@$user_login."%'";
		if (@$user_logout != "") $sql .= " AND user_logout like '%".@$user_logout."%'";
		if (@$user_log != "") $sql .= " AND user_log like '%".@$user_log."%'";
		if (@$groups_name != "") $sql .= " AND groups_name like '%".@$groups_name."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql." GROUP BY user_id ) k LIMIT ".(($page - 1) * $rows).",".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data,$divisi){
		$this->db->insert('s_user',$data);
		$id = $this->db->insert_id();
		
		/* update yang di centang 
		for($i=0; $i < count($divisi); $i++){
			$this->db->where('flowdet_id',$divisi[$i]);
			$this->db->update('m_flowdet',array("s_users_id" => $id));
		}*/
		
		return $id;
	}
	
	function _update($data,$id,$divisi){
		$this->db->where('user_id',$id);
		$this->db->update('s_user',$data);
		
		/* null kan flowdet s_users = user_id *
		$this->db->where('s_users_id',$id);
		$this->db->update('m_flowdet',array("s_users_id" => null));
		
		/* update yang di centang *
		for($i=0; $i < count($divisi); $i++){
			$this->db->where('flowdet_id',$divisi[$i]);
			$this->db->update('m_flowdet',array("s_users_id" => $id));
		} */
		
		return 1;
	}
	
	function _delete($data,$id){
		$this->db->where('user_id',$id);
		$this->db->update('s_user',$data);
        
		$this->db->where('s_users_id',$id);
		$this->db->update('m_flowdet',array("s_users_id" => null));
        
		return 1;
	}
	
	function s_groups(){
		$sql = "SELECT groups_id, groups_name FROM s_groups WHERE groups_active = 1";
		$query = $this->db->query($sql);
		$data = array("");
		foreach($query->result() as $row){
			$data[$row->groups_id] = $row->groups_name;
		}
		return $data;
	}
    
    function m_cabang(){
		$sql = "SELECT cabang_id, cabang_nama FROM m_cabang";
		$query = $this->db->query($sql);
		$data = array("");
		foreach($query->result() as $row){
			$data[$row->cabang_id] = $row->cabang_nama;
		}
		return $data;
    }
    
    function m_groupdiv(){
		$sql = "SELECT groupdiv_id, groupdiv_nama FROM m_groupdiv";
		$query = $this->db->query($sql);
		$data = array("");
		foreach($query->result() as $row){
			$data[$row->groupdiv_id] = $row->groupdiv_nama;
		}
		return $data;
    }
	
    function t_project(){
		$sql = "SELECT project_id, project_name FROM t_project";
		$query = $this->db->query($sql);
		$data = array("");
		foreach($query->result() as $row){
			$data[$row->project_id] = $row->project_name;
		}
		return $data;
    }
	function _listdiv(){
		$sql = "select flow_id, flow_name, flowdet_id, div_name, div_id, s_users_id from m_flowdet 
			 left join m_flow 
			on m_flow_id = flow_id
			 left join m_div 
			on m_div_id = div_id
			order by m_flow_id, flowdet_seq";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
    
    function _insertuserproject($data){
        $this->db->insert("t_userproject",$data);
    }
    
    function _clearuserproject($data){
        $this->db->where($data);
        $this->db->delete("t_userproject");
    }
}